<script setup lang="ts">
// 查询我的空间
import { listSpaceVoByPageUsingPost } from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { useUserStore } from '@/stores/user.ts'
import { useRouter } from 'vue-router'

import { onMounted } from 'vue'
import { SPACE_TYPE_ENUM } from '@/constants/space.ts'

const userStore = useUserStore()
const router = useRouter()

const checkAuth = async () => {
  //   判断是否登录
  if (!userStore.isLogin) {
    message.error('请先登录')
    router.replace('/user/login')
    return
  }
  const res = await listSpaceVoByPageUsingPost({
    spaceType: SPACE_TYPE_ENUM.PRIVATE,
    userId: userStore?.userInfo?.id,
  })
  let spaceId
  if (res.data.code === 0 && res.data.data) {
    spaceId = res.data.data?.records?.[0]?.id
  }
  console.log(spaceId)
  if (!spaceId) {
    message.error('请先创建空间')
    router.push('/space/add')
    return
  }
  router.push(`/space/${spaceId}`)
}

onMounted(() => {
  checkAuth()
})
</script>

<style scoped lang="scss"></style>
